package de.karlNet.sambamanager.ldapController.commands.macros;

import javax.naming.NamingException;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.context.annotation.ScopedProxyMode;
import org.springframework.stereotype.Controller;

import de.karlNet.sambamanager.ldapController.commands.BaseCommand;
import de.karlNet.sambamanager.ldapController.commands.SimpleModificationCommand;
import de.karlNet.sambamanager.ldapController.commands.attributeBuilder.ARecordBuilder;
import de.karlNet.sambamanager.ldapModel.dto.ModifyARecordDTO;

@Controller
@Scope(value = "session", proxyMode = ScopedProxyMode.TARGET_CLASS)
public class ModifyARecordMacro extends BaseCommand {
	@Autowired
	private ARecordBuilder aRecordBuilder;

	public void modifyARecord(ModifyARecordDTO modifyARecordDTO) throws NamingException {
		SimpleModificationCommand modificationCommand = new SimpleModificationCommand(
				this.ldapTemplate, modifyARecordDTO.getOldLdapDNSARecord()
						.getDn());
		modificationCommand.simpleModification(this.aRecordBuilder
				.buildARecordAttributeList(modifyARecordDTO
						.getNewLdapDNSARecord()));
		modificationCommand.execute();

		this.ldapTemplate.rename(modifyARecordDTO.getOldLdapDNSARecord()
				.getDn(), modifyARecordDTO.getNewLdapDNSARecord().getDn());
	}
}
